//
//  CPPredictVC.m
//  CPApp
//
//  Created by 健健 on 2018/3/21.
//  Copyright © 2018年 Class. All rights reserved.
//

#import "CPPredictVC.h"
#import "CPRightview.h"
#import "CPLeftTableCell.h"
#import "CPRightTableCell.h"
#import "CPPredictModel.h"

@interface CPPredictVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) CPRightview * rightView;
@property(nonatomic,assign) BOOL needRight;
@property(nonatomic,strong) UIView *back;
@property(nonatomic,assign) BOOL isRightHide;
@property(nonatomic,strong) NSString * headeTitle;
@property(nonatomic,strong) UITableView * leftTableView;
@property(nonatomic,strong) UITableView * rightTableView;
@property(nonatomic,strong) NSArray * leftDataArray;
@property(nonatomic,strong) NSMutableArray <CPPredictModel *>* dataArray;
@property(nonatomic,strong) NSArray * urlArray;
@property(nonatomic,assign) NSIndexPath *cellRow;
@property(nonatomic,strong) NSString * url;
@end

@implementation CPPredictVC
-(NSMutableArray <CPPredictModel *>*)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSArray *)urlArray {
    if (!_urlArray) {
        _urlArray= @[@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1038&lottype=1002&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=4ed96c593708e34b82dcbe3b9895d771&key=1522001774",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1039&lottype=1001&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=ab2fc0d74c779ff3d5cadb2ac7a13c6f&key=1522042906",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1040&lottype=1001&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=c28d0bf3bed360662f03c1af1af69cad&key=1522034846",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1041&lottype=1001&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=8121e6f253456e38d4836cd4319f0f0c&key=1521977802",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1042&lottype=1001&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=186b70e684531ea24db8feeec25dc515&key=1522035801",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1043&lottype=1001&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=541ab759b6516d01ad014bc42f5461b1&key=1522038950",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1044&lottype=1001&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=1e7d1b1122eaaa05b270e87009b68898&key=1522000102",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1045&lottype=1001&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=793ac4bd14daccd02260bb1d30e4e801&key=1522050475",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1046&lottype=1001&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=8c74bcf572297ac41e132a5549f201c2&key=1522049948",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1047&lottype=1001&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=6123e20c6c902e3911f6a3d50a9bcfe9&key=1522014542",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1037&lottype=1001&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=92609b60c4df4990f9d02bb5611c17ae&key=1522059969",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1050&lottype=1002&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=f6114657e3ff301f24dbec237b778381&key=1521992731",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1051&lottype=1002&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=ffb9fb82fe812516b33d77d80100470d&key=1521972495",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1052&lottype=1002&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=691221ec7c8c3522fda367fb9a0dfa8f&key=1522057141",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1053&lottype=1006&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=b4d717905df539b545032ba4dd1b6fbd&key=1522007502",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1054&lottype=1006&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=4f667b6c2b88b114bc38347ff45de9fd&key=1521975009",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1055&lottype=1004&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=f4c6f666366e6bf93be12dff1bf499f0&key=1522017283",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1056&lottype=1004&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=944235b7be1151ac079c269178d4b865&key=1522006641",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1057&lottype=1004&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=0f2fddcb3b20e3240018f951bd21d7aa&key=1522041857",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1058&lottype=1004&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=7d657b514ce5bd8f050520c4ff453a08&key=1521988627",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1059&lottype=1004&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=5b508d10f94c4fd73e9052403dea1e07&key=1522049577",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1060&lottype=1004&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=17444d1174de54cf3cc6e5f2d70d2911&key=1522009217",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1061&lottype=1004&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=aff061dd4a3f36c27dd1144c24622241&key=1522066898",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1048&lottype=1007&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=e0d997f4d2f4f7145c4fc1ff50202c7f&key=1522012343",@"http://115.29.175.83/cpyc/getcurcalc.php?lastid=-1&playtype=1049&lottype=1007&issuenum=7&userid=763294&ctype=1&iosversion=1.4.3&channel=sllottery_yc&token=64cb8c90cc6256a705135f6a970e8e1f&key=1522009134"];
    }
    return _urlArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"本期预测";
//    self.rightNavImageName = @"HomeRight";
//    self.headeTitle = @"双色球";
    [mNotificationCenter addObserver:self selector:@selector(predictNotification:) name:@"predictNotification" object:nil];
    self.leftDataArray = @[@"杀二码",@"杀三码",@"杀六码",@"杀十码",@"独胆",@"双胆",@"三胆",@"蓝球杀五码",@"蓝球定三胆",@"蓝球定五胆",@"蓝球杀号",@"个位杀二码",@"十位杀二码",@"百位杀二码",@"千位杀二码",@"万位杀二码",@"第一位杀二码",@"第二位杀二码",@"第三位杀二码",@"第四位杀二码",@"第五位杀二码",@"第六位杀二码",@"第七位杀二码",@"后区杀二码",@"后去区定六码"];
    self.url = self.urlArray[0];
    [self configWithTableView];
    [self configWithRefresh];
    [self loadDataWork];
    if (self.leftTableView) {
        self.cellRow = [NSIndexPath indexPathForRow:0 inSection:0];
    }
}
-(void)predictNotification:(NSNotification *)not {
//    NSString * tag = not.userInfo[@"tag"];
    self.needRight = NO;
    self.rightView.hidden = YES;
    self.back.hidden = YES;
//    self.headeTitle = not.userInfo[@"title"];
//    switch ([tag integerValue]) {
//        case 100:
//            self.leftDataArray = @[@"杀三码",@"杀六码",@"杀十码",@"独胆",@"双胆",@"三胆",@"蓝球杀五码",@"蓝球定三胆",@"蓝球定五胆",@"蓝球杀号"];
//            break;
//        case 101:
//            self.leftDataArray = @[@"杀二码",@"独胆",@"双胆",@"三胆",@"个位杀二码",@"十位杀二码",@"百位杀二码"];
//            break;
//        case 102:
//            self.leftDataArray = @[@"杀三码",@"杀六码",@"杀十码",@"独胆",@"双胆"];
//            break;
//        case 103:
//            self.leftDataArray = @[];
//            break;
//        case 104:
//            self.leftDataArray = @[@"杀二码",@"独胆",@"双胆",@"三胆",@"个位杀二码",@"十位杀二码",@"百位杀二码"];
//            break;
//        case 105:
//            self.leftDataArray = @[];
//            break;
//        case 106:
//            self.leftDataArray = @[@"杀三码",@"杀六码",@"杀十码",@"独胆",@"双胆"];
//            break;
//        default:
//            break;
//    }
    [_leftTableView reloadData];
}
-(void)configWithTableView {
    _leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 110, mScreen_H-TabBar_H-Nav_H-10) style:UITableViewStylePlain];
    _leftTableView.delegate = self;
    _leftTableView.dataSource = self;
    _leftTableView.tableFooterView = [UIView new];
//    [_leftTableView registerCellByNibName:@"CPLeftTableCell"];
    [self.view addSubview:_leftTableView];
    
    _rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(110, 0, mScreen_W-110, mScreen_H-TabBar_H-Nav_H-10) style:UITableViewStylePlain];
    _rightTableView.delegate = self;
    _rightTableView.dataSource = self;
    _rightTableView.tableFooterView = [UIView new];
    [_rightTableView registerCellByNibName:@"CPRightTableCell"];
    [self.view addSubview:_rightTableView];
    
}
-(void)configWithRefresh {
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshDataWithHeader)];
    self.rightTableView.mj_header = header;
}
- (void)refreshDataWithHeader {
    [self loadDataWork];
}
-(void)loadDataWork {
    [ProgressHUD showLoading];
    [HttpTool getApi:self.url params:nil success:^(id responseData) {
        [ProgressHUD dismiss];
        NSArray * arr = [CPPredictModel mj_objectArrayWithKeyValuesArray:responseData[@"list"]];
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:arr];
        [self.rightTableView endRefresh];
        [self.rightTableView reloadData];
    } failure:^(NSString *errorMsg) {
        [ProgressHUD show:errorMsg];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _leftTableView) {
        return self.leftDataArray.count;
    }else {
        return self.dataArray.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _leftTableView) {
        CPLeftTableCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle] loadNibNamed:@"CPLeftTableCell" owner:nil options:nil].firstObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.title.text = self.leftDataArray[indexPath.row];
        
        if (indexPath.row == self.cellRow.row) {
            cell.cellLine = YES;
        }
        if (cell.cellLine) {
            cell.line.hidden = NO;
        }else {
            cell.line.hidden = YES;
        }
        return cell;
    }else {
        CPRightTableCell * Cell = [tableView dequeueReusableCellWithIdentifier:@"CPRightTableCell"];
        Cell.model = self.dataArray[indexPath.row];
        return Cell;
    }
}
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    if (tableView ==_leftTableView) {
//        UIView * heade = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 110, 30)];
//        heade.backgroundColor = [UIColor clearColor];
//        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, heade.centerY-10, heade.width, 20)];
//        label.textColor = HJColor(240, 8, 7);
//        label.text = self.headeTitle;
//        label.font = [UIFont systemFontOfSize:17];
//        label.textAlignment = NSTextAlignmentCenter;
//        [heade addSubview:label];
//        return heade;
//    }
//    return [UIView new];
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    if (tableView==_leftTableView) {
//        return 30;
//    }
//    return 0.0001;
//}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _leftTableView) {
        return 40;
    }else {
        return 70;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _leftTableView) {
        CPLeftTableCell * cell = (CPLeftTableCell *)[tableView cellForRowAtIndexPath:indexPath];
        if (cell.cellLine) {
            return;
        }else {
            cell.cellLine = YES;
            cell.line.hidden = NO;
            self.url = _urlArray[indexPath.row];
            [self loadDataWork];
            if (self.cellRow) {
                CPLeftTableCell * selectCell = (CPLeftTableCell *)[tableView cellForRowAtIndexPath:self.cellRow];
                selectCell.cellLine = NO;
                selectCell.line.hidden = YES;
            }
        }
        self.cellRow = indexPath;
        
    }else {
        return;
    }
}

-(void)didClickRightNavAction {
    self.isRightHide = !self.isRightHide;
    if (self.isRightHide) {
        self.needRight = YES;
        self.rightView.hidden = NO;
        self.back.hidden = NO;
    }else {
        self.needRight = NO;
        self.rightView.hidden = YES;
        self.back.hidden = YES;
    }
    
}

-(void)setNeedRight:(BOOL)needRight {
    _needRight = needRight;
    if (needRight && !self.rightView && !self.back) {
        self.rightView = [[CPRightview alloc]init];
        self.rightView.frame = CGRectMake(0, 0, mScreen_W, 145);
        self.rightView.hidden = YES;
        self.rightView.navTitle = self.navTitle;
        [self.view addSubview:self.rightView];
        [self.view bringSubviewToFront:self.rightView];
        
        self.back = [UIView new];
        self.back.backgroundColor = [UIColor blackColor];
        self.back.alpha = 0.3;
        self.back.hidden = YES;
        self.back.frame = CGRectMake(0, CGRectGetMaxY(self.rightView.frame), mScreen_W, mScreen_H);
        [self.view addSubview:self.back];
        [self.view bringSubviewToFront:self.back];
    }
}
@end
